/*
 * {comment of class}
 *
 * @author (C) <a href="http://www.carfield.com.hk">Carfield Yim </a>, 2004
 */
package hk.com.carfield.core;

import hk.com.carfield.content.Code;
import hk.com.carfield.content.Content;
import hk.com.carfield.content.Flash;
import hk.com.carfield.content.Html;
import hk.com.carfield.content.IEFavorites;
import hk.com.carfield.content.Image;
import hk.com.carfield.content.MediaPlayer;
import hk.com.carfield.content.Real;
import hk.com.carfield.content.Text;
import hk.com.carfield.content.Unknown;
import hk.com.carfield.util.FileUtil;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public final class ContentManager {
	public static Content getContent(final Context context, final File file) {
		return context.cacheMan.getContent(context.config, file);
	}

	static Content getContent(final Config config, final File file) {
		try {
			final Class<?> itemClass = getType(file);
			if (itemClass != null && file.isFile()) {
				return (Content) itemClass.getConstructor(
						new Class[] { Config.class, File.class }).newInstance(
						new Object[] { config, file });
			} else {
				return new Unknown(config, file);
			}
		} catch (final IllegalArgumentException e) {
			throw new RuntimeException(e);
		} catch (final SecurityException e) {
			throw new RuntimeException(e);
		} catch (final InstantiationException e) {
			throw new RuntimeException(e);
		} catch (final IllegalAccessException e) {
			throw new RuntimeException(e);
		} catch (final InvocationTargetException e) {
			throw new RuntimeException(e);
		} catch (final NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}

	public static final String COMMENT = "comment";

	private static Class<? extends Content> CODE = Code.class,
			IMAGE = Image.class, MEDIA = MediaPlayer.class, TEXT = Text.class,
			XML = Code.class, REAL = Real.class, FLASH = Flash.class,
			SHTML = Html.class, URL = IEFavorites.class;

	private static Map<String, Class<? extends Content>> map = new HashMap<String, Class<? extends Content>>();
	static {
		map.put("jpg", IMAGE);
		map.put("gif", IMAGE);
		map.put("png", IMAGE);
		map.put("jpeg", IMAGE);
		map.put("jpe", IMAGE);
		map.put("bmp", IMAGE);
		// /map.put("tif", IMAGE);

		map.put("java", CODE);
		map.put("jsp", CODE);
		map.put("js", CODE);
		map.put("cpp", CODE);
		map.put("c", CODE);
		map.put("h", CODE);
		map.put("pl", CODE);
		map.put("pm", CODE);
		map.put("php", CODE);
		map.put("php3", CODE);
		map.put("php4", CODE);
		map.put("py", CODE);
		map.put("rb", CODE);
		map.put("awk", CODE);
		map.put("sh", CODE);
		map.put("tcsh", CODE);
		map.put("csh", CODE);
		map.put("ksh", CODE);
		map.put("bat", CODE);
		map.put("idl", CODE);
		map.put("sql", CODE);
		map.put("inf", CODE);

		// not really code... just use the syntax highligther to make it look
		// nicer
		map.put("properties", CODE);
		map.put("conf", CODE);
		map.put("jad", CODE);
		map.put("gmk", CODE); // make files
		map.put("jmk", CODE); // make files
		map.put("mk", CODE); // make files
		map.put("m4", CODE); // make files
		map.put("log", CODE);
		map.put("css", CODE);
		map.put("dtd", CODE);
		map.put("mf", CODE);
		map.put("list", CODE);
		map.put("cfg", CODE);
		map.put("ini", CODE);

		map.put("ra", REAL);
		map.put("rm", REAL);
		map.put("ram", REAL);

		map.put("wma", MEDIA);
		map.put("wmv", MEDIA);
		map.put("asf", MEDIA);
		map.put("asx", MEDIA);
		map.put("mpg", MEDIA);
		map.put("mpeg", MEDIA);
		map.put("mp3", MEDIA);
		map.put("avi", MEDIA);
		map.put("au", MEDIA);
		map.put("mov", MEDIA);
		map.put("mpe", MEDIA);

		map.put("txt", TEXT);
		map.put("1st", TEXT);
		map.put("inc", TEXT);

		map.put("xml", XML);
		map.put("xsd", XML);
		map.put("xsl", XML);

		map.put("swf", FLASH);
		map.put("url", URL);

		map.put("blog", SHTML);
		map.put(COMMENT, SHTML);
		map.put("shtml", SHTML);
		// TODO make browser for jar,pdb,zip,pdf later??
		// TODO pdf: http://www.google.com/search?q=java+read+pdf

	}

	public static Class<? extends Content> getType(final File file) {
		if (file.getName().equalsIgnoreCase("changelog")) {
			return CODE;
		}
		if (file.getName().equalsIgnoreCase("makefile")) {
			return CODE;
		}
		if (file.getName().indexOf(".") < 1) {
			return TEXT;
		}
		return map.get(FileUtil.getExtensions(file).toLowerCase());
	}

}